#include "CheckComboBox.h"
#include "QCheckBox"
CheckComboBox::CheckComboBox(QWidget * parent ) :QComboBox(parent)
{
    pListWidget = new QListWidget(this);
    pLineEdit = new QLineEdit(this);
    this->setModel(pListWidget->model());
    this->setView(pListWidget);
    this->setLineEdit(pLineEdit);
    pLineEdit->setReadOnly(true);
    //pLineEdit->setText("hhh");

}
CheckComboBox::~CheckComboBox()
{
    if (pListWidget)
    {
        delete pListWidget;
        pListWidget = nullptr;
    }
    if (pLineEdit)
    {
        delete pLineEdit;
        pLineEdit = nullptr;
    }
}

void CheckComboBox::setValues(QStringList values)
{
    for (auto value : values)
    {
        push_back(value);
    }
}
void CheckComboBox::push_back(QString value)
{
    QListWidgetItem *pItem = new QListWidgetItem(pListWidget);
    pLineEdit->setText("请选择您需要预约的医疗服务");
    pListWidget->addItem(pItem);
    QCheckBox *pCheckBox = new QCheckBox(this);
    pCheckBox->setText(value);
    pListWidget->addItem(pItem);
    pListWidget->setItemWidget(pItem, pCheckBox);
    connect(pCheckBox, SIGNAL(stateChanged(int)), this, SLOT(Slot_StateChanged(int)));
    pCheckBox->setChecked(false);
}

void CheckComboBox::Slot_StateChanged(int)
{
    /*
    QString  strSelectedData;
    int nCount = pListWidget->count();
    for (int i = 0; i < nCount; ++i)
    {
        QListWidgetItem *pItem = pListWidget->item(i);
        QWidget *pWidget = pListWidget->itemWidget(pItem);
        QCheckBox *pCheckBox = (QCheckBox *)pWidget;
        if (pCheckBox->isChecked())
        {
            QString strText = pCheckBox->text();
            strSelectedData.append(strText).append(";");
        }
    }
    //避免最后一个item添加";"
    if (strSelectedData.endsWith(";"))
        strSelectedData.remove(strSelectedData.count() - 1, 1);
    if (!strSelectedData.isEmpty())
    {
        pLineEdit->setText(strSelectedData);
        pLineEdit->setToolTip(strSelectedData);
    }
    */

    //pLineEdit->setText("fffffff");
//    pLineEdit->setToolTip("hhh");

}
QString CheckComboBox::text()
{
    return pLineEdit->text();
}
